﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Threading;

namespace RuoVea.ExUtil
{
    /// <summary>
    /// 描 述：常用公共类
    /// </summary>
    public static class Common
    {
        #region Stopwatch计时器
        /// <summary>
        /// 计时器开始
        /// </summary>
        /// <returns>Stopwatch 队列</returns>
        public static Stopwatch TimerStart()
        {
            Stopwatch watch = new Stopwatch();
            watch.Reset();
            watch.Start();
            return watch;
        }
        /// <summary>
        /// 计时器结束 
        /// </summary>
        /// <param name="watch">Stopwatch</param>
        /// <returns>毫秒数</returns>
        public static double TimerEnd(Stopwatch watch)
        {
            watch.Stop();
            double costtime = watch.ElapsedMilliseconds;
            return costtime;
        }
        #endregion

        #region 删除数组中的重复项
        /// <summary>
        /// 删除数组中的重复项
        /// </summary>
        /// <param name="values">重复值</param>
        /// <returns>返回重复项后的数组</returns>
        public static string[] RemoveDup(string[] values)
        {
            List<string> list = new List<string>();
            for (int i = 0; i < values.Length; i++)//遍历数组成员
            {
                if (!list.Contains(values[i]))
                {
                    list.Add(values[i]);
                };
            }
            return list.ToArray();
        }
        #endregion

        private static int _sn = 0;
        private static readonly object Locker = new object();

        #region 自动生成编号
        /// <summary>
        /// 自动生成编号  
        /// </summary>
        /// <returns>编号:201008251145409865</returns>
        public static string CreateNo()
        {
            lock (Locker)  //lock 关键字可确保当一个线程位于代码的临界区时，另一个线程不会进入该临界区。
            {
                if (_sn == int.MaxValue) _sn = 0; else _sn++;
                Thread.Sleep(100);
                Random random = new Random();
                string strRandom = random.Next(1000, 10000).ToString(); //生成编号
                string code = DateTime.Now.ToString("MMHHmmyyyyssdd") + strRandom;//形如
                return code;
            }
        }
        #endregion

        #region 生成Salt
        /// <summary>
        /// 生成 Salt 盐
        /// </summary>
        /// <param name="codeNum">生成长度</param>
        /// <returns>生成Salt盐</returns>
        public static string RndSalt(int codeNum)
        {
            StringBuilder sb = new StringBuilder(codeNum);
            string alphanum = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
            Random rand = new Random();

            for (int i = 1; i < codeNum + 1; i++)
            {
                int t = rand.Next(alphanum.Length);
                sb.AppendFormat("{0}", alphanum[t]);
            }
            return sb.ToString();
        }
        #endregion

        #region 生成0-9随机数
        /// <summary>
        /// 生成0-9随机数
        /// </summary>
        /// <param name="codeNum">生成长度</param>
        /// <returns>生成字符串的0-9随机数</returns>
        public static string RndNum(int codeNum)
        {
            StringBuilder sb = new StringBuilder(codeNum);
            Random rand = new Random();
            for (int i = 1; i < codeNum + 1; i++)
            {
                int t = rand.Next(9);
                sb.AppendFormat("{0}", t);
            }
            return sb.ToString();

        }
        #endregion
    }
}
